/**
 * @author Ahammer
 * @date 2009 06
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.media.jai.JAI;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSpinner;
import javax.swing.JToolBar;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.InternationalFormatter;
import main.Board;
import main.IqmTools;
import main.Tank;
import main.TankFrame;


public class IqmBorderGUI  extends IqmOperatorJFrame
						  implements IqmOperatorGUIBuilder, ActionListener, ChangeListener {

	private  ParameterBlockJAI   pbJAI         = null;  //  @jve:decl-index=0:
	//public  ImgProcFuncJFrame  opGUIFrame = null;
	//private  IqmBorderGUI 		 opGUIFrame = null;  //actionPerformed events werden in dieser Klasse abgehandelt
	
	private boolean isLastTimeChangedLeft   = false; //initially left will be changed at first when newWidth is changed
	private boolean isLastTimeChangedTop    = false; ///initially top will be changed at first when newHeightis changed
		
	private JPanel 				 	jPanelOldWidth				    = null;	
	private JLabel				 	jLabelOldWidth				    = null;
	private JFormattedTextField 	jFormattedTextFieldOldWidth     = null;	
	private JPanel 				 	jPanelOldHeight				    = null;	
	private JLabel				 	jLabelOldHeight				    = null;
	private JFormattedTextField 	jFormattedTextFieldOldHeight    = null;	
	
	private JPanel 				 	jPanelLeft		= null;	
	private JLabel				 	jLabelLeft		= null;
	private JSpinner				jSpinnerLeft	= null;
	
	private JPanel 				 	jPanelRight		= null;	
	private JLabel				 	jLabelRight		= null;
	private JSpinner				jSpinnerRight	= null;

	private JPanel 				 	jPanelTop		= null;	
	private JLabel				 	jLabelTop		= null;
	private JSpinner				jSpinnerTop		= null;

	private JPanel 				 	jPanelBottom	= null;	
	private JLabel				 	jLabelBottom	= null;
	private JSpinner				jSpinnerBottom	= null;
		
	private JPanel 				 	jPanelNewWidth		= null;	
	private JLabel				 	jLabelNewWidth		= null;
	private JSpinner			 	jSpinnerNewWidth 	= null;
	private JPanel 				 	jPanelNewHeight		= null;	
	private JLabel				 	jLabelNewHeight		= null;
	private JSpinner			 	jSpinnerNewHeight 	= null;
	
	private JButton					jButtonMaxImgSize   = null;  //to get and set maximal image width and height in an image stack
	
	private JPanel 				 	jPanelConst		= null;	
	private JLabel				 	jLabelConst		= null;
	private JSpinner			 	jSpinnerConst 	= null;

	private JRadioButtonMenuItem  buttZero      = null;	
	private JRadioButtonMenuItem  buttConst     = null;	
	private JRadioButtonMenuItem  buttCopy      = null;	
	private JRadioButtonMenuItem  buttReflect   = null;	
	private JRadioButtonMenuItem  buttWrap      = null;		
	private JRadioButtonMenuItem  buttMethodHidden   = null;
	private JToolBar              jToolBarMethod     = null;
	private ButtonGroup           buttGroupMethod    = null;
	
	private JRadioButtonMenuItem  buttBorder             = null;	//Preference for image stacks without same image sizes
	private JRadioButtonMenuItem  buttSize               = null;	
	private JRadioButtonMenuItem  buttBorderOrSizeHidden = null;
	private JToolBar              jToolBarBorderOrSize   = null;
	private ButtonGroup           buttGroupBorderOrSize  = null;
	private JPanel			      jPanelBorderOrSize     = null;
	private	JLabel				  jLabelBorderOrSize	 = null;
	
	/**
	 * constructor
	 */
	public IqmBorderGUI() {

	}

	
	/**
	 * This method sets all  variables to null
	 */
	private void resetVariables(){
		pbJAI         = null; 
		//opGUIFrame = null; 
		
		//jToolBarRGB  = null;

	}
	/**
	 * This method sets all parameters to null
	 */
	private void resetParameters(){
		//butt       = null;
		//butt       = null;
	}

	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pb
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pbJAI = arg;
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		
		pbJAI.setParameter("Left",      ((Number)jSpinnerLeft.getValue()).intValue());
		pbJAI.setParameter("Right",     ((Number)jSpinnerRight.getValue()).intValue());
		pbJAI.setParameter("Top",       ((Number)jSpinnerTop.getValue()).intValue());
		pbJAI.setParameter("Bottom",    ((Number)jSpinnerBottom.getValue()).intValue());
		pbJAI.setParameter("Const",     ((Number)jSpinnerConst.getValue()).intValue());
		
		pbJAI.setParameter("NewWidth",  ((Number)jSpinnerNewWidth.getValue()).intValue());
		pbJAI.setParameter("NewHeight", ((Number)jSpinnerNewHeight.getValue()).intValue());
		
		if (buttZero.isSelected())     pbJAI.setParameter("Method", 0);
		if (buttConst.isSelected())    pbJAI.setParameter("Method", 1);
		if (buttCopy.isSelected())     pbJAI.setParameter("Method", 2);
		if (buttReflect.isSelected())  pbJAI.setParameter("Method", 3);
		if (buttWrap.isSelected())     pbJAI.setParameter("Method", 4);
	
		if (buttBorder.isSelected()) pbJAI.setParameter("BorderOrSize", 0);
		if (buttSize.isSelected())   pbJAI.setParameter("BorderOrSize", 1);
			
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	
	/**
	 * This method sets the current parameter values
	 *
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmBorderGUI: Current image not defined");
	    	   return;
	       }
        
	    jSpinnerLeft.removeChangeListener(this);
	    jSpinnerRight.removeChangeListener(this);	    	    
	    jSpinnerTop.removeChangeListener(this);	    	    
	    jSpinnerBottom.removeChangeListener(this);	    	     	    
	    jSpinnerConst.removeChangeListener(this);
	    
	    jSpinnerLeft.setValue(pbJAI.getIntParameter("Left"));
	    jSpinnerRight.setValue(pbJAI.getIntParameter("Right"));
	    jSpinnerTop.setValue(pbJAI.getIntParameter("Top"));
	    jSpinnerBottom.setValue(pbJAI.getIntParameter("Bottom"));
	    jSpinnerConst.setValue(pbJAI.getIntParameter("Const"));
	    
	    jSpinnerLeft.addChangeListener(this);
	    jSpinnerRight.addChangeListener(this);
	    jSpinnerTop.addChangeListener(this);
	    jSpinnerBottom.addChangeListener(this);
	    jSpinnerConst.addChangeListener(this);
	    		
	    jSpinnerNewWidth.removeChangeListener(this);	
	    jSpinnerNewHeight.removeChangeListener(this);    
	    jSpinnerNewWidth.setValue(pbJAI.getIntParameter("NewWidth"));
	    jSpinnerNewHeight.setValue(pbJAI.getIntParameter("NewHeight"));
	    jSpinnerNewWidth.addChangeListener(this);
	    jSpinnerNewHeight.addChangeListener(this);
	    
		if (pbJAI.getIntParameter("Method") == 0) buttZero.setSelected(true);
		if (pbJAI.getIntParameter("Method") == 1) buttConst.setSelected(true);
		if (pbJAI.getIntParameter("Method") == 2) buttCopy.setSelected(true);
		if (pbJAI.getIntParameter("Method") == 3) buttReflect.setSelected(true);
		if (pbJAI.getIntParameter("Method") == 4) buttWrap.setSelected(true);
		
		if (buttConst.isSelected()) {
			jSpinnerConst.setEnabled(true);
		} else {
			jSpinnerConst.setEnabled(false);
		}
		
		buttBorderOrSizeHidden.setSelected(true);
		if (pbJAI.getIntParameter("BorderOrSize") == 0)  buttBorder.setSelected(true);
		if (pbJAI.getIntParameter("BorderOrSize") == 1)  buttSize.setSelected(true);

	}
	/**
	 * This method updates the GUI
	 * This method overrides IqmOperatorJFrame
	 */
	@Override
	public void updateIqmOperatorGUI(){
		int idx = IqmTools.getCurrManagerImgIndex();
		PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	    if (pi == null){
	    	System.out.println("IqmBorder: Current image not defined");
	    	return;
	    }
//		if (pbJAI == null) pbJAI = new ParameterBlockJAI("iqmborder");  //nur wenn das erste mal aufgerufen
//		//pbJAI = new ParameterBlockJAI("iqmborder"); //immer der default Block
//		IqmTools.setCurrParameterBlockJAI(pbJAI);
//		
//		this.setParameterValues();
		
	    jFormattedTextFieldOldWidth.setValue(pi.getWidth());
	    jFormattedTextFieldOldHeight.setValue(pi.getHeight());	    
		int left   = ((Number)jSpinnerLeft.getValue()).intValue();
		int right  = ((Number)jSpinnerRight.getValue()).intValue();
		int top    = ((Number)jSpinnerTop.getValue()).intValue();
		int bottom = ((Number)jSpinnerBottom.getValue()).intValue();

		
//		//Set image dependent initial values;
	    jSpinnerNewWidth.removeChangeListener(this);
	    jSpinnerNewWidth.setValue(pi.getWidth() + left + right);
	    jSpinnerNewWidth.addChangeListener(this);
	    jSpinnerNewHeight.removeChangeListener(this);
	    jSpinnerNewHeight.setValue(pi.getHeight() + top + bottom);	
	    jSpinnerNewHeight.addChangeListener(this);
		this.setParameterBlock();		
		this.getJContentPane().revalidate();		
		this.setParameterValues();	
		

	}
	/**
	 * This method creates and shows the GUI
	 */
	@Override
	public void createIqmOperatorGUI() {
		//System.out.println("IqmBorder: createAndShowGUI invoked");
		//opGUIFrame = new IqmBorderGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("Border");
		PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());	
		
		//this.setSize(dim.width, dim.height+280);
		//this.setPreferredSize(new Dimension(800, 600));

		this.getJContentPane().add(getJPanelOldWidth(),  getGridBagConstraintsOldWidth());
		this.getJContentPane().add(getJPanelOldHeight(), getGridBagConstraintsOldHeight());
		this.getJContentPane().add(getJPanelLeft(),      getGridBagConstraintsLeft());
		this.getJContentPane().add(getJPanelRight(),     getGridBagConstraintsRight());
		this.getJContentPane().add(getJPanelTop(),       getGridBagConstraintsTop());
		this.getJContentPane().add(getJPanelBottom(),    getGridBagConstraintsBottom());
		this.getJContentPane().add(getJPanelNewWidth(),  getGridBagConstraintsNewWidth());
		this.getJContentPane().add(getJPanelNewHeight(), getGridBagConstraintsNewHeight());
		this.getJContentPane().add(getJButtonMaxImgSize(),getGridBagConstraintsMaxImgSize());		
		this.getJContentPane().add(getJPanelConst(),     getGridBagConstraintsConst());
		this.getJContentPane().add(getJToolBarMethod(),  getGridBagConstraintsButtonMethodGroup());
		this.getJContentPane().add(getJPanelBorderOrSize(),   getGridBagConstraintsBorderOrSize());
		this.pack();
		//this.repaint();
		
	      
		if (pbJAI == null) pbJAI = new ParameterBlockJAI("IqmBorder");  //nur wenn das erste mal aufgerufen
		//pbJAI = new ParameterBlockJAI("iqmthreshold"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pbJAI);
		this.setParameterValues();

		//Set image dependent initial values;
//	    jFormattedTextFieldOldWidth.setValue(pi.getWidth());
//	    jFormattedTextFieldOldHeight.setValue(pi.getHeight());	    
//	    jSpinnerNewWidth.removeChangeListener(this);
//	    jSpinnerNewWidth.setValue(pi.getWidth());
//	    jSpinnerNewWidth.addChangeListener(this);
//	    jSpinnerNewHeight.removeChangeListener(this);
//	    jSpinnerNewHeight.setValue(pi.getHeight());	
//	    jSpinnerNewHeight.addChangeListener(this);
//	    
		//this.setParameterBlock();
	    this.updateIqmOperatorGUI();

		
	}
	
	private GridBagConstraints getGridBagConstraintsOldWidth(){
		GridBagConstraints gridBagConstraintsOldWidth = new GridBagConstraints();
		gridBagConstraintsOldWidth.gridx = 1;
		gridBagConstraintsOldWidth.gridy = 5;
		gridBagConstraintsOldWidth.gridwidth = 1;//?
		gridBagConstraintsOldWidth.insets = new Insets(10,0,0,0); //top left bottom right
		//gridBagConstraintsOldWidth.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOldWidth;
	}
	private GridBagConstraints getGridBagConstraintsOldHeight(){
		GridBagConstraints gridBagConstraintsOldHeight = new GridBagConstraints();
		gridBagConstraintsOldHeight.gridx = 2;
		gridBagConstraintsOldHeight.gridy = 5;
		gridBagConstraintsOldHeight.gridwidth = 2;//?
		gridBagConstraintsOldHeight.insets = new Insets(10,20,0,0);  //top left bottom right
		//gridBagConstraintsOldHeight.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOldHeight;
	}
	private GridBagConstraints getGridBagConstraintsTop(){
		GridBagConstraints gridBagConstraintsTop = new GridBagConstraints();
		gridBagConstraintsTop.gridx = 1;
		gridBagConstraintsTop.gridy = 6;
		gridBagConstraintsTop.gridwidth = 3;//?
		gridBagConstraintsTop.insets = new Insets(30,0,0,0);  //top left bottom right
		//gridBagConstraintsTop.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsTop;
	}
	private GridBagConstraints getGridBagConstraintsLeft(){
		GridBagConstraints gridBagConstraintsLeft = new GridBagConstraints();
		gridBagConstraintsLeft.gridx = 1;
		gridBagConstraintsLeft.gridy = 7;
		gridBagConstraintsLeft.gridwidth = 1;//?
		gridBagConstraintsLeft.insets = new Insets(20,0,0,0);  //top left bottom right
		//gridBagConstraintsLeft.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsLeft;
	}
	private GridBagConstraints getGridBagConstraintsRight(){
		GridBagConstraints gridBagConstraintsRight = new GridBagConstraints();
		gridBagConstraintsRight.gridx = 2;
		gridBagConstraintsRight.gridy = 7;
		gridBagConstraintsRight.gridwidth = 2;//?
		gridBagConstraintsRight.insets = new Insets(20,0,0,0);  //top left bottom right
		//gridBagConstraintsRight.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsRight;
	}

	private GridBagConstraints getGridBagConstraintsBottom(){
		GridBagConstraints gridBagConstraintsBottom = new GridBagConstraints();
		gridBagConstraintsBottom.gridx = 1;
		gridBagConstraintsBottom.gridy = 8;
		gridBagConstraintsBottom.gridwidth = 3;//?
		gridBagConstraintsBottom.insets = new Insets(20,0,0,0);  //top left bottom right
		//gridBagConstraintsBottom.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsBottom;
	}
	private GridBagConstraints getGridBagConstraintsNewWidth(){
		GridBagConstraints gridBagConstraintsNewWidth = new GridBagConstraints();
		gridBagConstraintsNewWidth.gridx = 1;
		gridBagConstraintsNewWidth.gridy = 9;
		gridBagConstraintsNewWidth.gridwidth = 1;//?
		gridBagConstraintsNewWidth.insets = new Insets(30,0,0,0);  //top left bottom right
		//gridBagConstraintsNewWidth.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsNewWidth;
	}
	private GridBagConstraints getGridBagConstraintsNewHeight(){
		GridBagConstraints gridBagConstraintsNewHeight = new GridBagConstraints();
		gridBagConstraintsNewHeight.gridx = 2;
		gridBagConstraintsNewHeight.gridy = 9;
		gridBagConstraintsNewHeight.gridwidth = 2;//?
		gridBagConstraintsNewHeight.insets = new Insets(30,0,0,0);  //top left bottom right
		//gridBagConstraintsNewHeight.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsNewHeight;
	}
	private GridBagConstraints getGridBagConstraintsMaxImgSize(){
		GridBagConstraints gridBagConstraintsMaxImgSize = new GridBagConstraints();
		gridBagConstraintsMaxImgSize.gridx = 1;
		gridBagConstraintsMaxImgSize.gridy = 10;
		gridBagConstraintsMaxImgSize.gridwidth = 3;//?
		gridBagConstraintsMaxImgSize.insets = new Insets(30,10,0,0);  //top left bottom right
		//gridBagConstraintsMaxImgSize.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsMaxImgSize;
	}
	private GridBagConstraints getGridBagConstraintsConst(){
		GridBagConstraints gridBagConstraintsConst = new GridBagConstraints();
		gridBagConstraintsConst.gridx = 1;
		gridBagConstraintsConst.gridy = 11;
		gridBagConstraintsConst.gridwidth = 1;//?
		gridBagConstraintsConst.insets = new Insets(10,00,0,0);  //top left bottom right
		//gridBagConstraintsConst.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsConst;
	}
	private GridBagConstraints getGridBagConstraintsButtonMethodGroup(){
		GridBagConstraints gridBagConstraintsButtonMethodGroup = new GridBagConstraints();
		gridBagConstraintsButtonMethodGroup.gridx = 2;
		gridBagConstraintsButtonMethodGroup.gridy = 11;
		gridBagConstraintsButtonMethodGroup.gridwidth = 2;//?
		gridBagConstraintsButtonMethodGroup.insets = new Insets(10,0,20,0);  //top left bottom right		
		//gridBagConstraintsButtonMethodGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonMethodGroup;
	}
	private  GridBagConstraints getGridBagConstraintsBorderOrSize(){
		GridBagConstraints gridBagConstraintsButtonBorderOrSizeGroup = new GridBagConstraints();
		gridBagConstraintsButtonBorderOrSizeGroup.gridx = 1;
		gridBagConstraintsButtonBorderOrSizeGroup.gridy = 12;
		gridBagConstraintsButtonBorderOrSizeGroup.gridwidth = 3;//?
		//gridBagConstraintsButtonBorderOrSizeGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonBorderOrSizeGroup;
	}	
	//------------------------------------------------------------------------------------------------------------------
	/**
	 * This method closes and destroys the GUI
	 * 
	 * @return void
	 */
//	public void destroyGUI() {
//		if (this != null){
//					
//			//this.closeAndDestroyGUI();
//			//resetVariables();
//			//folgendes geht auch!?
//			this.setVisible(false);
//			this.dispose();
//			//this = null;
//		}
//	}


	//-----------------------------------------------------------------------
	class IntNumberVerifier extends InputVerifier { //damit muss Eingabe richtig sein 
		@Override
		public boolean verify(JComponent input) {
		   JFormattedTextField ftf = (JFormattedTextField)input;
		   JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter();
		   if (formatter != null) {
		      String text = ftf.getText();
		  	  try {
				   //text = text.replace(",", ".");
				   Integer.valueOf(text);
				   //Float.valueOf(text);
					return true;
			  } catch (NumberFormatException e) {
					return false;
			  }	 
		   }
		   return true;
		}
//		public boolean shouldYieldFocus(JComponent input) {
//		System.out.println("NumberVerifier  shouldYieldFocus");
//
//          return verify(input);
//    }
}
	/**
	 * This method initializes jJPanelOldWidth	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelOldWidth() {
		if (jPanelOldWidth == null) {
			jPanelOldWidth = new JPanel();
			jPanelOldWidth.setLayout(new BorderLayout());
			//jPanelOldWidth.setPreferredSize(new Dimension(250,18));					
			jLabelOldWidth = new JLabel("Old Width: ");
			jLabelOldWidth.setPreferredSize(new Dimension(65,18));
			jLabelOldWidth.setHorizontalAlignment(JLabel.RIGHT);
			jFormattedTextFieldOldWidth = new JFormattedTextField(NumberFormat.getNumberInstance());
			//jFormattedTextFieldOldWidth.setPreferredSize(new Dimension(70,18));
			//jFormattedTextFieldOldWidth.addPropertyChangeListener("value", this);
			//jFormattedTextFieldOldWidth.setInputVerifier(new IntNumberVerifier());
			jFormattedTextFieldOldWidth.setEditable(false);
			//jFormattedTextFieldOldWidth.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);	
			jFormattedTextFieldOldWidth.setColumns(5);
			InternationalFormatter intFormatter = (InternationalFormatter)jFormattedTextFieldOldWidth.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			jPanelOldWidth.add(jLabelOldWidth, 			  BorderLayout.WEST);
			jPanelOldWidth.add(jFormattedTextFieldOldWidth, BorderLayout.CENTER);	
		
		}
		return jPanelOldWidth;
	}
	/**
	 * This method initializes jJPanelOldHeight	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelOldHeight() {
		if (jPanelOldHeight == null) {
			jPanelOldHeight = new JPanel();
			jPanelOldHeight.setLayout(new BorderLayout());
			//jPanelOldHeight.setPreferredSize(new Dimension(250,18));					
			jLabelOldHeight = new JLabel("Old Height: ");
			jLabelOldHeight.setPreferredSize(new Dimension(65,18));
			jLabelOldHeight.setHorizontalAlignment(JLabel.RIGHT);
			jFormattedTextFieldOldHeight = new JFormattedTextField(NumberFormat.getNumberInstance());
			//jFormattedTextFieldOldHeight.setPreferredSize(new Dimension(70,18));
			//jFormattedTextFieldOldHeight.addPropertyChangeListener("value", this);
			//jFormattedTextFieldOldHeight.setInputVerifier(new IntNumberVerifier());
			jFormattedTextFieldOldHeight.setEditable(false);
			//jFormattedTextFieldOldHeight.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);	
			jFormattedTextFieldOldHeight.setColumns(5);
			InternationalFormatter intFormatter = (InternationalFormatter)jFormattedTextFieldOldHeight.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			jPanelOldHeight.add(jLabelOldHeight, 			  BorderLayout.WEST);
			jPanelOldHeight.add(jFormattedTextFieldOldHeight, BorderLayout.CENTER);
			jFormattedTextFieldOldHeight.setEnabled(true);	
		}
		return jPanelOldHeight;
	}

	/**
	 * This method initializes jJPanelLeft	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelLeft() {
		if (jPanelLeft == null) {
			jPanelLeft = new JPanel();
			jPanelLeft.setLayout(new BorderLayout());		
			jLabelLeft = new JLabel("Left: ");
			jLabelLeft.setPreferredSize(new Dimension(50, 22));
			jLabelLeft.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, -Integer.MAX_VALUE, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerLeft = new JSpinner(sModel);
			jSpinnerLeft.setPreferredSize(new Dimension(60, 22));
			jSpinnerLeft.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerLeft.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelLeft.add(jLabelLeft, 	 BorderLayout.WEST);
			jPanelLeft.add(jSpinnerLeft, BorderLayout.CENTER);			
		}
		return jPanelLeft;
	}
	/**
	 * This method initializes jJPanelRight	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelRight() {
		if (jPanelRight == null) {
			jPanelRight = new JPanel();
			jPanelRight.setLayout(new BorderLayout());	
			jLabelRight = new JLabel("Right: ");
			jLabelRight.setPreferredSize(new Dimension(40, 22));
			jLabelRight.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, -Integer.MAX_VALUE, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerRight = new JSpinner(sModel);
			jSpinnerRight.setPreferredSize(new Dimension(60, 22));
			jSpinnerRight.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerRight.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			
			jPanelRight.add(jLabelRight, 	 BorderLayout.WEST);
			jPanelRight.add(jSpinnerRight, BorderLayout.CENTER);			
		}
		return jPanelRight;
	}
	/**
	 * This method initializes jJPanelTop	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelTop() {
		if (jPanelTop == null) {
			jPanelTop = new JPanel();
			jPanelTop.setLayout(new BorderLayout());			
			jLabelTop = new JLabel("Top: ");
			jLabelTop.setPreferredSize(new Dimension(45, 22));
			jLabelTop.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, -Integer.MAX_VALUE, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerTop = new JSpinner(sModel);
			jSpinnerTop.setPreferredSize(new Dimension(60, 22));
			jSpinnerTop.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerTop.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			
			jPanelTop.add(jLabelTop, 	 BorderLayout.WEST);
			jPanelTop.add(jSpinnerTop, BorderLayout.CENTER);			
		}
		return jPanelTop;
	}
	/**
	 * This method initializes jJPanelBottom	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelBottom() {
		if (jPanelBottom == null) {
			jPanelBottom = new JPanel();
			jPanelBottom.setLayout(new BorderLayout());	
			jLabelBottom = new JLabel("Bottom: ");
			jLabelBottom.setPreferredSize(new Dimension(50, 22));
			jLabelBottom.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, -Integer.MAX_VALUE, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerBottom = new JSpinner(sModel);
			jSpinnerBottom.setPreferredSize(new Dimension(60, 22));
			jSpinnerBottom.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerBottom.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			
			jPanelBottom.add(jLabelBottom, 	 BorderLayout.WEST);
			jPanelBottom.add(jSpinnerBottom, BorderLayout.CENTER);			
		}
		return jPanelBottom;
	}
	//--------------------------------------------------------------------------------------------------
//	/**
//	 * This method initializes jJPanelNewWidth	
//	 * 	
//	 * @return javax.swing.JPanel
//	 */
//	private JPanel getJPanelNewWidth() {
//		if (jPanelNewWidth == null) {
//			jPanelNewWidth = new JPanel();
//			jPanelNewWidth.setLayout(new BorderLayout());				
//			jLabelNewWidth = new JLabel("New Width: ");
//			jLabelNewWidth.setPreferredSize(new Dimension(70,18));
//			jLabelNewWidth.setHorizontalAlignment(JLabel.RIGHT);
//			jFormattedTextFieldNewWidth = new JFormattedTextField(NumberFormat.getNumberInstance());
//			//jFormattedTextFieldNewWidth.setPreferredSize(new Dimension(70,18));
//			//jFormattedTextFieldNewWidth.addPropertyChangeListener("value", this);
//			//FormattedTextFieldNewWidth.setInputVerifier(new IntNumberVerifier());
//			jFormattedTextFieldNewWidth.setEditable(false);
//			//jFormattedTextFieldNewWidth.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);	
//			jFormattedTextFieldNewWidth.setColumns(5);
//			InternationalFormatter intFormatter = (InternationalFormatter)jFormattedTextFieldNewWidth.getFormatter() ;
//			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
//			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
//			jPanelNewWidth.add(jLabelNewWidth, 			  BorderLayout.WEST);
//			jPanelNewWidth.add(jFormattedTextFieldNewWidth, BorderLayout.CENTER);	
//			jFormattedTextFieldNewWidth.setEnabled(true);
//		}
//		return jPanelNewWidth;
//	}
//	/**
//	 * This method initializes jJPanelNewHeight	
//	 * 	
//	 * @return javax.swing.JPanel
//	 */
//	private JPanel getJPanelNewHeight() {
//		if (jPanelNewHeight == null) {
//			jPanelNewHeight = new JPanel();
//			jPanelNewHeight.setLayout(new BorderLayout());
//			//jPanelNewHeight.setPreferredSize(new Dimension(250,18));					
//			jLabelNewHeight = new JLabel("New Height: ");
//			jLabelNewHeight.setPreferredSize(new Dimension(70,18));
//			jLabelNewHeight.setHorizontalAlignment(JLabel.RIGHT);
//			jFormattedTextFieldNewHeight = new JFormattedTextField(NumberFormat.getNumberInstance());
//			//jFormattedTextFieldNewHeight.setPreferredSize(new Dimension(70,18));
//			//jFormattedTextFieldNewHeight.addPropertyChangeListener("value", this);
//			//jFormattedTextFieldNewHeight.setInputVerifier(new IntNumberVerifier());
//			jFormattedTextFieldNewHeight.setEditable(false);
//			//jFormattedTextFieldNewHeight.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);	
//			jFormattedTextFieldNewHeight.setColumns(5);
//			InternationalFormatter intFormatter = (InternationalFormatter)jFormattedTextFieldNewHeight.getFormatter() ;
//			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
//			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
//			jPanelNewHeight.add(jLabelNewHeight, 			  BorderLayout.WEST);
//			jPanelNewHeight.add(jFormattedTextFieldNewHeight, BorderLayout.CENTER);	
//			jFormattedTextFieldNewHeight.setEnabled(true);
//
//		}
//		return jPanelNewHeight;
//	}
	//-------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jJPanelNewWidth	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelNewWidth() {
		if (jPanelNewWidth == null) {
			jPanelNewWidth = new JPanel();
			jPanelNewWidth.setLayout(new BorderLayout());		
			jLabelNewWidth = new JLabel("New Width: ");
			jLabelNewWidth.setPreferredSize(new Dimension(70, 22));
			jLabelNewWidth.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerNewWidth = new JSpinner(sModel);
			jSpinnerNewWidth.setPreferredSize(new Dimension(60, 22));
			jSpinnerNewWidth.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNewWidth.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelNewWidth.add(jLabelNewWidth, 	 BorderLayout.WEST);
			jPanelNewWidth.add(jSpinnerNewWidth, BorderLayout.CENTER);			
		}
		return jPanelNewWidth;
	}
	/**
	 * This method initializes jJPanelNewHeight	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelNewHeight() {
		if (jPanelNewHeight == null) {
			jPanelNewHeight = new JPanel();
			jPanelNewHeight.setLayout(new BorderLayout());		
			jLabelNewHeight = new JLabel("New Height: ");
			jLabelNewHeight.setHorizontalAlignment(JLabel.RIGHT);
			jLabelNewHeight.setPreferredSize(new Dimension(70, 22));
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerNewHeight = new JSpinner(sModel);
			jSpinnerNewHeight.setPreferredSize(new Dimension(60, 22));
			jSpinnerNewHeight.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNewHeight.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelNewHeight.add(jLabelNewHeight,   BorderLayout.WEST);
			jPanelNewHeight.add(jSpinnerNewHeight, BorderLayout.CENTER);			
		}
		return jPanelNewHeight;
	}
	//-------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonMaxImgSize() {
		if (jButtonMaxImgSize == null) {
			jButtonMaxImgSize = new JButton("Get Maximal Image Size");
			//jButtonMaxImgSize.setPreferredSize(new Dimension(50, 22));
			jButtonMaxImgSize.setToolTipText("gets the maximal image size, mainly for an image stack");
			jButtonMaxImgSize.addActionListener(this);
			jButtonMaxImgSize.setActionCommand("parameter");
		}
		return jButtonMaxImgSize;
	}
	//-------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jJPanelConst	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelConst() {
		if (jPanelConst == null) {
			jPanelConst = new JPanel();
			jPanelConst.setLayout(new BorderLayout());
			jLabelConst = new JLabel("Const: ");
			jLabelConst.setPreferredSize(new Dimension(50, 22));
			jLabelConst.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, 255, 1); //init, min, max, step
			jSpinnerConst = new JSpinner(sModel);
			jSpinnerConst.setPreferredSize(new Dimension(60, 22));
			jSpinnerConst.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerConst.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			
			jPanelConst.add(jLabelConst, 	 BorderLayout.WEST);
			jPanelConst.add(jSpinnerConst, BorderLayout.CENTER);			
		}
		return jPanelConst;
	}
	
	//--------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtZero() {
		if (buttZero == null) {
			buttZero  = new JRadioButtonMenuItem();
			buttZero .setText("Zero");
			buttZero .setToolTipText("filling borders with zeros");
			buttZero .addActionListener(this);
			buttZero.setActionCommand("parameter");
		}
		return buttZero;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtConst() {
		if (buttConst == null) {
			buttConst  = new JRadioButtonMenuItem();
			buttConst .setText("Constant");
			buttConst .setToolTipText("filling borders with a user defined constant");
			buttConst .addActionListener(this);
			buttConst.setActionCommand("parameter");
		}
		return buttConst;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtCopy() {
		//if (buttCopy == null) {
			buttCopy  = new JRadioButtonMenuItem();
			buttCopy .setText("Copy");
			buttCopy .setToolTipText("filling borders with a copy of the edge pixels");
			buttCopy .addActionListener(this);
			buttCopy.setActionCommand("parameter");
		//}
		return buttCopy;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtReflect() {
		//if (buttReflect == null) {
			buttReflect  = new JRadioButtonMenuItem();
			buttReflect .setText("Reflect");
			buttReflect .setToolTipText("filling borders with copies of the whole image");
			buttReflect .addActionListener(this);
			buttReflect.setActionCommand("parameter");
		//}
		return buttReflect;
	}
	/**
	 * This method initializes the Option: 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtWrap() {
		//if (buttWrap == null) {
			buttWrap  = new JRadioButtonMenuItem();
			buttWrap .setText("Wrap");
			buttWrap .setToolTipText("filling borders with copies of the whole image");
			buttWrap .addActionListener(this);
			buttWrap.setActionCommand("parameter");
		//}
		return buttWrap;
	}

	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMethodHidden() {
		//if (buttMethodHidden == null) {
			buttMethodHidden  = new JRadioButtonMenuItem();
			buttMethodHidden .setText("Hidden");
			buttMethodHidden.setVisible(false);
		//}
		return buttMethodHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarMethod() {
//		if (jToolBarMethod== null) {
			jToolBarMethod = new JToolBar("Options");
			jToolBarMethod.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarMethod.add(getJRadioButtonMenuButtZero());
			jToolBarMethod.add(getJRadioButtonMenuButtConst());
			jToolBarMethod.add(getJRadioButtonMenuButtCopy());
			jToolBarMethod.add(getJRadioButtonMenuButtReflect());
			jToolBarMethod.add(getJRadioButtonMenuButtWrap());
			jToolBarMethod.add(getJRadioButtonMenuButtMethodHidden());
			//jToolBarMethod.addSeparator();
			this.setButtonGroupMethod();	//Gruppieren der JRadioButtons
//		}
		return jToolBarMethod;
	}
	private  void setButtonGroupMethod() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupMethod = new ButtonGroup();
			buttGroupMethod.add(buttZero);
			buttGroupMethod.add(buttConst);
			buttGroupMethod.add(buttCopy);
			buttGroupMethod.add(buttReflect);
			buttGroupMethod.add(buttWrap);
			buttGroupMethod.add(buttMethodHidden);
	}
	//--------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option:
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtBorder() {
		//if (buttBorder == null) {
			buttBorder  = new JRadioButtonMenuItem();
			buttBorder .setText("Border");
			buttBorder .setToolTipText("prefers the border settings instead of the new size values");
			buttBorder .addActionListener(this);
			buttBorder.setActionCommand("parameter");
		//}
		return buttBorder;
	}
	/**
	 * This method initializes the Option:
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtSize() {
		//if (buttSize == null) {
			buttSize  = new JRadioButtonMenuItem();
			buttSize .setText("New Size");
			buttSize .setToolTipText("prefers the new size setting instead of the zoom values");
			buttSize .addActionListener(this);
			buttSize.setActionCommand("parameter");
		//}
		return buttSize;
	}

	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtBorderOrSizeHidden() {
		//if (buttBorderOrSizeHidden == null) {
			buttBorderOrSizeHidden  = new JRadioButtonMenuItem();
			buttBorderOrSizeHidden .setText("Hidden");
			buttBorderOrSizeHidden.setVisible(false);
		//}
		return buttBorderOrSizeHidden;
	}


//	/**
//	 * This method initializes jJToolBarBar	
//	 * 	
//	 * @return javax.swing.JToolBar	
//	 */
//	private   JToolBar getJToolBarBorderOrSize() {
////		if (jToolBarBorderOrSize== null) {
//			jToolBarBorderOrSize = new JToolBar("Options");
//			jToolBarBorderOrSize.setOrientation(1); // 0 Horizontal 1 Vertical orientation
//			jToolBarBorderOrSize.add(getJRadioButtonMenuButtBorder());
//			jToolBarBorderOrSize.add(getJRadioButtonMenuButtSize());
//			jToolBarBorderOrSize.add(getJRadioButtonMenuButtBorderOrSizeHidden());
//			//jToolBarBorderOrSize.addSeparator();
//			this.setButtonGroupBorderOrSize();	//Gruppieren der JRadioButtons
////		}
//		return jToolBarBorderOrSize;
//	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private JPanel getJPanelBorderOrSize() {
//		if (jToolBarBorderOrSize== null) {
			jPanelBorderOrSize = new JPanel();
			jPanelBorderOrSize.setLayout(new BorderLayout());	
			jLabelBorderOrSize = new JLabel("Preference if image size varies in a stack : ");
			jToolBarBorderOrSize = new JToolBar("Options");
			jToolBarBorderOrSize.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarBorderOrSize.add(getJRadioButtonMenuButtBorder());
			jToolBarBorderOrSize.add(getJRadioButtonMenuButtSize());
			jToolBarBorderOrSize.add(getJRadioButtonMenuButtBorderOrSizeHidden());
			//jToolBarBorderOrSize.addSeparator();
			this.setButtonGroupBorderOrSize();	//Gruppieren der JRadioButtons
			jPanelBorderOrSize.add(jLabelBorderOrSize, 	 BorderLayout.WEST);
			jPanelBorderOrSize.add(jToolBarBorderOrSize, BorderLayout.CENTER);			
//		}
		return jPanelBorderOrSize;
	}


	private void setButtonGroupBorderOrSize() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupBorderOrSize = new ButtonGroup();
			buttGroupBorderOrSize.add(buttBorder);
			buttGroupBorderOrSize.add(buttSize);
			buttGroupBorderOrSize.add(buttBorderOrSizeHidden);
	}
 	//--------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmBorderGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmBorder".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {
			if (buttConst == e.getSource()) {
				jSpinnerConst.setEnabled(true);
			}else{
				jSpinnerConst.setEnabled(false);
			}
			
			if (jButtonMaxImgSize == e.getSource()) {
				//System.out.println("IqmBorderGUI: jButtonMaxImgSize Event");
				//Scroll through all images
				Board.appendTexln("IqmBorderGUI Looking for largest image width and height, please wait.....");
				int maxWidth = 0;
				int maxHeight = 0;
				//int curr = IqmTools.getCurrTankIndex();
				//DefaultListModel dlm = Tank.getCurrentTankDataAt(curr);
					
				boolean virtFlag = false;
				int curr = IqmTools.getCurrTankIndex();
				if (curr ==-1){
					System.out.println("IqmBorderGUI: Current tank index not available");
					return;
				}

				try {
					if (((DefaultListModel) TankFrame.model.getElementAt(curr)).getElementAt(0) instanceof PlanarImage) virtFlag = false;
					if (((DefaultListModel) TankFrame.model.getElementAt(curr)).getElementAt(0) instanceof File)        virtFlag = true;
				} catch (ArrayIndexOutOfBoundsException e1) {
					System.out.println("IqmBorderGUI: Current image index not available");
					return;
				}

				PlanarImage pi = null;			
				for (int i = 0; i < (((DefaultListModel) TankFrame.model.getElementAt(curr))).size(); i++){
					
					try {
						if (!virtFlag){
							pi = (PlanarImage) ((DefaultListModel) TankFrame.model.getElementAt(curr)).getElementAt(i);
						}
						if (virtFlag) {
							File file = (File) ((DefaultListModel) TankFrame.model.getElementAt(curr)).getElementAt(i);
							pi = JAI.create("fileload", file.toString());
			        		pi.setProperty("file_name",  file.toString());
			        		pi.setProperty("image_name", file.getName().toString());
						}

					} catch (ArrayIndexOutOfBoundsException e1) {
						System.out.println("IqmBorderGUI: Current image not available");
						return;
					}
					maxWidth  = Math.max(maxWidth,  pi.getWidth());
					maxHeight = Math.max(maxHeight, pi.getHeight());
				}
				Board.appendTexln("IqmBorderGUI Largest width: "+maxWidth+"  Largest height: "+ maxHeight);
				jSpinnerNewWidth.setValue(maxWidth);    //initiates change events
				jSpinnerNewHeight.setValue(maxHeight);	//initiates change events			
			}
	
			this.setParameterBlock();
		}
		this.updateIqmOperatorGUI();  //if necessary here or some lines above
	
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	//-----------------------------------------------------------------------------------------------

//	@Override
//	public void propertyChange(PropertyChangeEvent e) {
//		//System.out.println("IqmBorderGUI: PropertyChangeEvent");
//		int left      = ((Number)jSpinnerLeft.getValue()).intValue();
//		int right     = ((Number)jSpinnerRight.getValue()).intValue();
//		int top       = ((Number)jSpinnerTop.getValue()).intValue();
//		int bottom    = ((Number)jSpinnerBottom.getValue()).intValue();
//		int newWidth  = ((Number)jSpinnerNewWidth.getValue()).intValue();
//		int newHeight = ((Number)jSpinnerNewHeight.getValue()).intValue();
//		int oldWidth  = ((Number)jFormattedTextFieldOldWidth.getValue()).intValue();
//		int oldHeight = ((Number)jFormattedTextFieldOldHeight.getValue()).intValue();
//		int c         = ((Number)jSpinnerConst.getValue()).intValue();
//		
//		if (jFormattedTextFieldOldWidth == e.getSource()) {
//		}
//		if (jFormattedTextFieldOldHeight  == e.getSource()) {			
//		}
//
//	
//		if (jSpinnerNewWidth  == e.getSource()) {
//			if(newWidth < oldWidth) {
//				jSpinnerLeft.setValue(oldWidth);
//				jSpinnerLeft.setValue(0);
//				jSpinnerRight.setValue(0);
//			}
//		}
//		if (jSpinnerNewHeight  == e.getSource()) {
//			if(newHeight < oldHeight) {
//				jSpinnerNewHeight.setValue(oldHeight);
//				jSpinnerTop.setValue(0);
//				jSpinnerBottom.setValue(0);
//			}
//		}
//		jSpinnerNewWidth.setValue(oldWidth + left + right);
//		jSpinnerNewHeight.setValue(oldHeight + top + bottom);
//	
//		//this.setParameterBlock();
//		this.updateIqmOperatorGUI();  //if necessary here or some lines above
//		//preview if selected
//		if (this.isAutoPreviewSelected()){
//			this.showPreview();
//		}
//	}
	//-------------------------------------------------------------------------------------
	@Override
	public void stateChanged(ChangeEvent e) {
		//System.out.println("IqmBorderGUI: ChangeEvent");
		int left      = ((Number)jSpinnerLeft.getValue()).intValue();
		int right     = ((Number)jSpinnerRight.getValue()).intValue();
		int top       = ((Number)jSpinnerTop.getValue()).intValue();
		int bottom    = ((Number)jSpinnerBottom.getValue()).intValue();
		int newWidth  = ((Number)jSpinnerNewWidth.getValue()).intValue();
		int newHeight = ((Number)jSpinnerNewHeight.getValue()).intValue();
		int oldWidth  = ((Number)jFormattedTextFieldOldWidth.getValue()).intValue();
		int oldHeight = ((Number)jFormattedTextFieldOldHeight.getValue()).intValue();
		int c         = ((Number)jSpinnerConst.getValue()).intValue();
		
		if (jSpinnerLeft == e.getSource() || jSpinnerRight  == e.getSource()) {
			
			if ((oldWidth + left + right) > 0){
				jSpinnerNewWidth.removeChangeListener(this);
				jSpinnerNewWidth.setValue(oldWidth + left + right);
				jSpinnerNewWidth.addChangeListener(this);
			}
		}
		if (jSpinnerTop  == e.getSource() || jSpinnerBottom == e.getSource()) {	
			if ((oldHeight + top + bottom) > 0){
				jSpinnerNewHeight.removeChangeListener(this);
				jSpinnerNewHeight.setValue(oldHeight + top + bottom);
				jSpinnerNewHeight.addChangeListener(this);
			}
		}	
		
		if (jSpinnerNewWidth  == e.getSource()) {
			while (newWidth > (oldWidth + left +right)) {
				if (isLastTimeChangedLeft){			
					right = right + 1;
					jSpinnerRight.removeChangeListener(this);
					jSpinnerRight.setValue(right);
					jSpinnerRight.addChangeListener(this);
					isLastTimeChangedLeft  = false;
				} else {	
					left = left + 1;
					jSpinnerLeft.removeChangeListener(this);
					jSpinnerLeft.setValue(left);
					jSpinnerLeft.addChangeListener(this);
					isLastTimeChangedLeft  = true;;			
				}					
			}
			while (newWidth < (oldWidth + left +right)) {
				if (isLastTimeChangedLeft){			
					right = right - 1;
					jSpinnerRight.removeChangeListener(this);
					jSpinnerRight.setValue(right);
					jSpinnerRight.addChangeListener(this);
					isLastTimeChangedLeft  = false;
				} else {	
					left = left - 1;
					jSpinnerLeft.removeChangeListener(this);
					jSpinnerLeft.setValue(left);
					jSpinnerLeft.addChangeListener(this);
					isLastTimeChangedLeft  = true;;			
				}					
			}
		}
		if (jSpinnerNewHeight  == e.getSource()) {
			while (newHeight > (oldHeight + top +bottom)) {
				if (isLastTimeChangedTop){			
					bottom = bottom + 1;
					jSpinnerBottom.removeChangeListener(this);
					jSpinnerBottom.setValue(bottom);
					jSpinnerBottom.addChangeListener(this);
					isLastTimeChangedTop  = false;
				} else {	
					top = top + 1;
					jSpinnerTop.removeChangeListener(this);
					jSpinnerTop.setValue(top);
					jSpinnerTop.addChangeListener(this);
					isLastTimeChangedTop  = true;;			
				}					
			}
			while (newHeight < (oldHeight + top +bottom)) {
				if (isLastTimeChangedTop){			
					bottom = bottom - 1;
					jSpinnerBottom.removeChangeListener(this);
					jSpinnerBottom.setValue(bottom);
					jSpinnerBottom.addChangeListener(this);
					isLastTimeChangedTop  = false;
				} else {	
					top = top - 1;
					jSpinnerTop.removeChangeListener(this);
					jSpinnerTop.setValue(top);
					jSpinnerTop.addChangeListener(this);
					isLastTimeChangedTop  = true;;			
				}					
			}
		}
		this.setParameterBlock();
		this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
		
	}

	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmBorderGUI gui = new IqmBorderGUI();
				gui.createIqmOperatorGUI();

			}
		});
	}


	
}//END
